﻿using System;
using System.Linq;
using Core.Domain.Interfaces;

namespace Core.Domain.Bases
{
  ///<summary>Base for repositories</summary>
  ///<typeparam name="T"></typeparam>
  public abstract class EntityRepository<T> : IRepository<T> where T : EntityBase
  {
    protected readonly IPersistanceRepository _repository;

    ///<summary></summary>
    ///<param name="repository"></param>
    protected EntityRepository(IPersistanceRepository repository)
    {
      _repository = repository;
    }

    #region IRepository<T> Members

    public virtual void Save(T entity)
    {
      if (entity == null)
      {
        throw new ArgumentNullException("entity");
      }

      if (entity.IsValid)
      {
        _repository.Save(entity);
      }
    }

    public void Add(T entity)
    {
      if(entity.IsValid)
      {
        
      }
    }

    public void Remove(T entity)
    {
      throw new NotImplementedException();
    }

    #endregion

    ///<summary>Get by Id</summary>
    ///<param name="id"></param>
    ///<returns></returns>
    public virtual T Get(int id)
    {
      return _repository.Get<T>(id);
    }

    protected IQueryable<T> Query()
    {
      return _repository.Query<T>();
    }
  }
}